<?php

namespace app\store\service;

use app\store\model\ApplyOfflineShop;
use app\store\model\Banner;
use app\store\model\OfflineShop;
use think\facade\Db;

class OfflineShopService
{
    /**
     * 更新店铺状态
     */
    public static function updateStatus($data) {
        $store_id = session('store_id');
        $model = new OfflineShop();
        $rst = $model->where('store_id',$store_id)->where('id', $data['id'])->update(['s_status'=>$data['status']]);
        // echo $model->getLastSql();exit;
        return $rst;
    }

    /**
     * 推荐店铺
     */
    public static function updateTJ($data){
        $store_id = session('store_id');
        $model = new OfflineShop();
        $rst = $model->where('store_id',$store_id)->where('id', $data['id'])->update(['is_recommended'=>$data['status']]);
        // echo $model->getLastSql();exit;
        return $rst;
    }

    /**
     * 审核店铺
     */
    public function auditShop($status, $id, $uid)
    {
        Db::startTrans();
        $store_id = session('store_id');
        try {
            if ($status == 2) {
                Db::name('user')->where(['store_id'=>$store_id, 'user_id'=>$uid])->update(['type'=>2]);
            }
            $auditRst = Db::name('apply_offline_shop')->where('id', $id)->where('store_id',$store_id)->find();
            if (empty($auditRst)) {
                return false;
            }
            $type = $auditRst['type'];//1、入驻 2、修改
            if ($type == 1) {
                Db::name('offline_shop')->where('id', $auditRst['offline_shop_id'])->where('store_id',$store_id)->update(['status'=>$status, 'update_time'=>time()]);
                Db::name('apply_offline_shop')->where('id', $id)->where('store_id',$store_id)->update(['status'=>$status, 'update_time'=>time()]);    
            } else {
                $offline_shop_id = $auditRst['offline_shop_id'];
                Db::name('apply_offline_shop')->where('id', $id)->where('store_id',$store_id)->update(['status'=>$status, 'update_time'=>time()]);    
                $shopModel = OfflineShop::find($offline_shop_id);
                $shopModel->allowField(['category_id', 'logo','label_id','location_address','bg_imgs','lat','lng',
                'legal_person','contact_phone','shop_name','province','city','area','address','business_license'])->save($auditRst);
            }
             Db::commit();
            return true;
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return false;
        }
    }

    /**
     * 门店详情已审核过的
     */
    public function findSHOne($id) {
        $store_id = session('store_id');
        $model = new OfflineShop();
        $list = $model->where('store_id',$store_id)->where('id', $id)->find();
        $bg_img = explode(',',$list['bg_imgs']);
        $list['bg_imgs'] = $bg_img;
        return  $list;
    }

    /**
     * 门店详情
     */
    public function findOne($id)
    {
        $store_id = session('store_id');
        $model = new ApplyOfflineShop();
        $list = $model->where('store_id',$store_id)->where('id', $id)->find();
        $bg_img = explode(',',$list['bg_imgs']);
        $list['bg_imgs'] = $bg_img;
        return  $list;
    }

    /**
     * 查找待审核的门店
     */
    public function auditList()
    {
        $store_id = session('store_id');
        $model = new ApplyOfflineShop();
        $list = $model->where('store_id',$store_id)->where('status', 1)->order('create_time','desc')->paginate(10);
        return  $list;
    }

     /**
     * 查找已审核的门店
     */
    public function shopList()
    {
        $store_id = session('store_id');
        $model = new OfflineShop();
        $list = $model->where('store_id',$store_id)->where('status', 2)->order('create_time','desc')->paginate(10);
        return  $list;
    }
    
    // public static function getBannerList()
    // {   
    //     $store_id = session('store_id');

    //     $model = new Banner();
    //     $banners = $model->where('store_id',$store_id)->order('sort','asc')->select();
        
    //     return  $banners;
        
    // }

    // public static function saveBanner($data)
    // {
    //    $model = new Banner();
    //    $res =  $model->saveAll($data);

    //    return $res;
    // }

    // public static function deleteBanner($id)
    // {
    //     $res =  Db::name('banner')->where('id',$id)->delete();

    //     return $res;
    // }
    
}